package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.applicationsecurity.mapper.AlarmRecordMapper;
import com.yl.applicationsecurity.mapper.AssetsMapper;
import com.yl.applicationsecurity.mapper.SgmSafeRiskMapper;
import com.yl.applicationsecurity.pojo.AlarmRecord;
import com.yl.applicationsecurity.pojo.Assets;
import com.yl.applicationsecurity.pojo.SgmSafeRisk;
import com.yl.applicationsecurity.service.AlarmRecordService;
import com.yl.applicationsecurity.service.AssetsService;
import com.yl.applicationsecurity.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-04-30 10:49:57
 */
@Service
public class AlarmRecordServiceImpl  extends ServiceImpl<AlarmRecordMapper, AlarmRecord> implements AlarmRecordService {


    @Autowired
    private SgmSafeRiskMapper sgmSafeRiskMapper;

    @Override
    public void getDataSgmSafeRiskToAlarmRecord(LocalDateTime now) {
        /**
         1. 每天统计一次告警记录
         2. 统计的范围包括两部分
         2.1 统计当天推送的告警记录(SgmSafeRisk)，并将对应SgmSafeRisk的内容插入到AlarmRecord表格中
         2.2 统计当天人工审计的告警记录(SgmSafeRisk)，并将对应SgmSafeRisk的内容插入到AlarmRecord表格中
         2.3 记录当前待处理的告警记录，并判断是否超期，如果超期，则将告警记录的状态改为超期

         3. 判断是否有超期，如果有超期则将告警记录的状态改为超期
         **/

        //2.1 统计当天(24小时前)推送的告警记录(SgmSafeRisk)和处于待处理的告警记录，并将对应SgmSafeRisk的内容插入到AlarmRecord表格中
        // 24 小时前
        LocalDateTime twentyFourHoursAgo = now.minusHours(24);

        // 转换为 java.util.Date 类型
        Date nowDate = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        Date twentyFourHoursAgoDate = Date.from(twentyFourHoursAgo.atZone(ZoneId.systemDefault()).toInstant());

        // 构造查询条件
        LambdaQueryWrapper<SgmSafeRisk> sgmSafeRiskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sgmSafeRiskLambdaQueryWrapper.between(
                SgmSafeRisk::getLastSystemProcessingDate,
                twentyFourHoursAgoDate,
                nowDate
        ).or().between(
                SgmSafeRisk::getLastDisposalDate,
                twentyFourHoursAgoDate,
                nowDate
        ).or()
                .eq(SgmSafeRisk::getStatus, 0);

        List<SgmSafeRisk> sgmSafeRiskList =  sgmSafeRiskMapper.selectList(sgmSafeRiskLambdaQueryWrapper);
        HashSet<SgmSafeRisk> set = new HashSet<>(sgmSafeRiskList);

        AlarmRecord alarmRecord = null;
        for (SgmSafeRisk sgmSafeRisk : set) {
            alarmRecord = new AlarmRecord();
            alarmRecord.setId(UUID.randomUUID().toString());
            alarmRecord.setRecordId(sgmSafeRisk.getId().toString());
            alarmRecord.setCountry(sgmSafeRisk.getCountry());
            alarmRecord.setType(1);//越权告警

            if(sgmSafeRisk.getStatus()!=0&&sgmSafeRisk.getLastDisposalDate()!=null
                    &&sgmSafeRisk.getOverdueDate()!=null&&sgmSafeRisk.getLastDisposalDate().after(sgmSafeRisk.getOverdueDate())){ //判断超期的条件为两种   第一种是 状态为待处理的 处置时间>处置时效
                alarmRecord.setStatus(1);
            }else if(sgmSafeRisk.getStatus()==0 &&sgmSafeRisk.getOverdueDate()!=null&&sgmSafeRisk.getOverdueDate().before(new Date())){ //第二种是 状态不为待处理的 当前时间>处置时效
                alarmRecord.setStatus(1);
            }else{
                alarmRecord.setStatus(0);
            }
            alarmRecord.setStatisticsDate(new Date());
            baseMapper.insert(alarmRecord);

        }


    }

    @Override
    public HashMap<String, Object> getDataReportSgmSafeRiskToAlarmRecord(String[] nationSelect, Date statisticsDate,  boolean isStatisticsDate) {
        HashMap<String, Object> map = new HashMap<>();
        Date stateDate =  DateUtils.getFirstDayOfMonth(statisticsDate);//获取当月的第一天
       // Date endDate =  DateUtils.getLastMomentOfMonth(statisticsDate);//获取当月的最后一天
        Date endDate = null;
        if(!isStatisticsDate){
            endDate =  DateUtils.getLastMomentOfMonth(statisticsDate);//获取当月的最后一天
        }else{
            endDate = statisticsDate;//将传入时间作为查询截止时间

        }

        //获取当月告警总数量
        long momentCount = baseMapper.selectCountByDate(stateDate,endDate,nationSelect);
        map.put("momentCount", momentCount);
        //获取当月超期告警数量
        long momentoverdueDateCount = baseMapper.selectCountByDateAndStatus(stateDate,endDate,1,nationSelect);
        map.put("momentoverdueDateCount", momentoverdueDateCount);

        //获取当月 31天的告警数量趋势
        List<Long> countDetails = new ArrayList<>();
        List<Long> overdueDateCountDetails = new ArrayList<>();
        Integer days = DateUtils.getElapsedOrTotalDays(statisticsDate);//根据查询时间获取当月已经过了的天数
        long c = 0;
        for (int i = 1; i <= days; i++) {
            c = baseMapper.selectCountByDate(stateDate,DateUtils.addDays(stateDate,i),nationSelect);
            countDetails.add(c);
            c = baseMapper.selectCountByDateAndStatus(stateDate,DateUtils.addDays(stateDate,i),1,nationSelect);
            overdueDateCountDetails.add(c);
        }
        map.put("countDetails",countDetails);
        map.put("overdueDateCountDetails",overdueDateCountDetails);
        return map;
    }
}

